namespace WXWorkSvc.Areas.Admin.Pages.Jwt;

public class EditModel : PageModel
{
    private readonly WXDbContext db;
    public EditModel(WXDbContext db)
    {
        this.db = db;
    }
    [BindProperty]
    public TJwt Item { get; set; } = default!;
    public async Task OnGet(int id)
    {
        if (id == 0)
            Item = new TJwt() { Secret = Guid.NewGuid().ToString() };
        else
            Item = await db.Jwts.FirstAsync(x => x.Id == id);
    }

    public async Task<IActionResult> OnPostAsync()
    {
        if (Item.IsDelete)
        {
            if (Item.Id > 0)
                await db.Jwts.Where(ii => ii.Id == Item.Id).ExecuteDeleteAsync();
        }
        else
        {
            db.Jwts.Attach(Item).State = Item.Id == 0 ? EntityState.Added : EntityState.Modified;
            await db.SaveChangesAsync();
        }
        return RedirectToPage("./Index");
    }
}
